import {
  getEventsApi,
  getMeasuresApi,
  getPropertiesEventApi,
  getPropertiesUserApi,
  getEventValueApi,
  getUserValueApi,
  getUserValuesByProjectIdApi,
  getEventValuesByProjectIdApi,
  getMetaUserPropertiesApi, getPropertiesOfUserTagApi,
} from '@/api/analyseType'

const analyseType = {
  namespaced: true,
  state: {
    filterForm: {},
    events: [],
    measures: [],
    propertiesEvent: [],
    propertiesUser: []
  },

  mutations: {
    SET_FILTERFORM: (state, form) => {
      state.filterForm = form
    },
    SET_EVENTS: (state, data) => {
      state.events = data
    },
    SET_MEASURES: (state, data) => {
      state.measures = data
    },
    SET_PROPERTIESEVENT: (state, data) => {
      state.propertiesEvent = data
    },
    SET_PROPERTIESUSER: (state, data) => {
      state.propertiesUser = data
    }
  },

  actions: {
    getEventValues ( {commit, rootState}, propertyName ) {
      return new Promise((resolve, reject) => {
        getEventValueApi(propertyName)
          .then(res => {
            if (res.code == 0) {
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    getEventValuesByProjectId( {commit, rootState}, data ) {
      return new Promise((resolve, reject) => {
        getEventValuesByProjectIdApi(data)
          .then(res => {
            if (res.code == 0) {
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    getUserValues ( {commit, rootState}, propertyName ) {
      return new Promise((resolve, reject) => {
        getUserValueApi(propertyName)
          .then(res => {
            if (res.code == 0) {
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    getUserValuesByProjectId({commit, rootState}, data) {
      return new Promise((resolve, reject) => {
        getUserValuesByProjectIdApi(data)
          .then(res => {
            if (res.code == 0) {
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    getEvents ( {commit, rootState}, id) {
      const datasourceId = id ? id:rootState.custom.reportForm.datasourceId
      return new Promise((resolve, reject) => {
        getEventsApi({ id: datasourceId })
          .then(res => {
            if (res.code == 0) {
              commit('SET_EVENTS', res.data )
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
        })
      },
    getMeasures ( {commit, rootState}, id) {
      const datasourceId = id ? id:rootState.custom.reportForm.datasourceId
      return new Promise((resolve, reject) => {
        getMeasuresApi({ id: datasourceId })
          .then(res => {
            if (res.code == 0) {
              commit('SET_MEASURES', res.data)
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    getPropertiesEvent ( {commit, rootState}, id) {
      const datasourceId = id ? id:rootState.custom.reportForm.datasourceId
      return new Promise((resolve, reject) => {
        getPropertiesEventApi({ id: datasourceId})
          .then(res => {
            if (res.code == 0) {
              commit('SET_PROPERTIESEVENT', res.data)
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    getPropertiesUser ( {commit, rootState}, id) {
      const datasourceId = id ? id: rootState.custom.reportForm.datasourceId
      return new Promise((resolve, reject) => {
        getPropertiesUserApi({ id: datasourceId})
          .then(res => {
            if (res.code == 0) {
              commit('SET_PROPERTIESUSER', res.data)
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    getPropertiesOfUserTag ( {commit, rootState}, id) {
      const datasourceId = id ? id: rootState.custom.reportForm.datasourceId
      return new Promise((resolve, reject) => {
        getPropertiesOfUserTagApi({ id: datasourceId}).then(res => {
          if (res.code === 0) {
            resolve(res)
          } else {
            reject(res.msg)
          }
        }).catch(err => {
          reject(err)
        })
      })
    },
    getMetaUserProperties ( {commit, rootState}, id) {
      return new Promise((resolve, reject) => {
        getMetaUserPropertiesApi({ datasourceId: id})
          .then(res => {
            if (res.code == 0) {
              resolve(res.data)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    getPropertiesEvent_report ({ commit }, datasetId) {
      return new Promise((resolve, reject) => {
        getPropertiesEventApi(datasetId)
          .then(res => {
            if (res.code == 0) {
              commit('SET_PROPERTIESEVENT', res.data)
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    getPropertiesUser_report ({commit}, datasetId) {
      return new Promise((resolve, reject) => {
        getPropertiesUserApi(datasetId)
          .then(res => {
            if (res.code == 0) {
              commit('SET_PROPERTIESUSER', res.data)
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    getEvent_report ({commit}, datasetId) {
      return new Promise((resolve, reject) => {
        getEventsApi(datasetId)
          .then(res => {
            if (res.code == 0) {
              commit('SET_EVENTS', res.data)
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    }
  },

  getters: {

  }
}

export default analyseType
